var createPublicKey = function (obj) {
    var $obj = $(obj);
    var id = $obj.attr('id');
    var value = $obj.val();
    if (!confirm('upload '+value+' ?')){
        $obj.val('');
        return false;
    }
    $.ajaxFileUpload({
        url:'/pgpauth/publickey/create/',
        async: true,
        fileElementId: id,
        dataType: 'json',
        success: function (json) {
            $('#file').val('');
            if (json['status'] != true){
                alert(json['message']);
                return false;
            } else {
                if (json['user']) {
                    window.location = './';
                }
                $('#id').html('<a href="/pgpauth/account/create/'+json['id']+'/">click me to create account</a>');
                $('#fingerprint').text(json['fingerprint']);
                $('#email').text(json['email']);
                return true;
            }
        },
        error: function (json, status, e) {
            var msg = '';
            for (var k in json) {
                msg += k + ':' + json[k] + ',';
            }
            var content = 'ajaxFileUpload: '+ status + ', ' + e + ': ' + msg;
            var $ajaxError = $('<div id="ajaxError" class="flora">'+content+'</div>');
            $('body').append($ajaxError);
            $ajaxError.dialog({
                title: '',
                //modal: true,
                //overlay: {opacity: 0.8, background: "black"},
                buttons: {
                    'close': function(){
                        $ajaxError.dialog('close');
                    }
                },
                width: 950,
                height: 550
            });
            return false;
        }
    });
    return false;
}

var createAccount = function (obj) {
    var username = $('#username').val();
    var signed_content = $('#signed_content').val();
    if (! username || ! signed_content) {
        alert('please type [username] and [signed_content]!');
    } else {
        $.post('/pgpauth/account/create/', {'username': username,
            'signed_content': signed_content}, function(json){
            if (json['status'] != true) {
                alert(json['message']);
            } else {
                window.location = json['next'];
            }
        }, 'json');
    }
}

var loginByPGP = function (obj) {
    var securesentence_id = $('#securesentence_id').val();
    var signed_content = $('#signed_content').val();
    if (! securesentence_id ) {
        alert('the login session was expired, please reload the login page!');
    } else if (! signed_content) {
        alert('please type [signed_content]!');
    } else {
        $.post('/pgpauth/pgplogin/', {'securesentence_id': securesentence_id,
            'signed_content': signed_content}, function(json){
            if (json['status'] != true) {
                $('#securesentence_id').val(json['securesentence_id']);
                $('#securesentence_content').text(json['securesentence_id']+'.'+json['securesentence_content']);;
                $('#signed_content').text(json['securesentence_id']+'.'+json['securesentence_content']);;
                alert(json['message']);
            } else {
                window.location = json['next'];
            }
        }, 'json');
    }
}